package com.sskj.aikeeper.hangqing.activity;

import android.annotation.SuppressLint;
import android.app.DialogFragment;
import android.content.Context;
import android.content.Intent;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.alibaba.android.arouter.launcher.ARouter;
import com.scwang.smartrefresh.header.MaterialHeader;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.sskj.aikeeper.R;
import com.sskj.lib.SPConfig;
import com.sskj.lib.SpApplication;
import com.sskj.aikeeper.presenter.SettingPresenter;
import com.sskj.common.base.App;
import com.sskj.common.base.AppManager;
import com.sskj.common.util.ClickUtil;
import com.sskj.common.util.LanguageUtil;
import com.sskj.common.util.SPUtil;
import com.sskj.lib.Constans;
import com.sskj.lib.RConfig;
import com.sskj.lib.base.BaseActivity;
import com.sskj.lib.bean.AppVersionBean;
import com.sskj.lib.util.APKVersionCodeUtils;
import com.sskj.lib.widget.BottomDialog;
import com.sskj.lib.widget.MyClasscsFooter;

import java.util.Locale;

import butterknife.BindView;
import sskj.lee.appupdatelibrary.BaseUpdateDialogFragment;
import sskj.lee.appupdatelibrary.BaseVersion;

/**设置：版本更新、中英文
 * @author Hey
 * Create at  2019/06/25
 */
public class SettingActivity extends BaseActivity<SettingPresenter> {

    @BindView(R.id.tvTitle)
    TextView tvTitle;
    @BindView(R.id.tv_version)
    TextView tvVersion;
    @BindView(R.id.ll_version)
    LinearLayout llVersion;
    @BindView(R.id.tv_language)
    TextView tvLanguage;
    @BindView(R.id.ll_language)
    LinearLayout llLanguage;

    @Override
    public int getLayoutId() {
        return R.layout.app_activity_setting;
    }

    @Override
    public SettingPresenter getPresenter() {
        return new SettingPresenter();
    }

    @SuppressLint("SetTextI18n")
    @Override
    public void initView() {
        tvTitle.setText(App.INSTANCE.getString(R.string.app_settingActivity1));
        tvVersion.setText("V" + APKVersionCodeUtils.getVerName(SettingActivity.this));

        if (Constans.LANGUAGE_EN.equals(SPUtil.get(SPConfig.LANGUAGE, Constans.LANGUAGE_CNA))) {
            tvLanguage.setText("English");
        } else if (Constans.LANGUAGE_CNA.equals(SPUtil.get(SPConfig.LANGUAGE, Constans.LANGUAGE_CNA))) {
            tvLanguage.setText(App.INSTANCE.getString(R.string.lib_lib_bottom_dialog_language20));
        }else if (Constans.LANGUAGE_TRA.equals(SPUtil.get(SPConfig.LANGUAGE, Constans.LANGUAGE_CNA))) {
            tvLanguage.setText(App.INSTANCE.getString(R.string.lib_lib_bottom_dialog_language40));
        }
    }

    @Override
    public void initData() {
        ClickUtil.click(300, llVersion, () -> {
            mPresenter.checkVersion(APKVersionCodeUtils.getVerName(SettingActivity.this));
        });
        ClickUtil.click(300, llLanguage, () -> {
            BottomDialog.changeLanguge(SettingActivity.this, "", new BottomDialog.OnSure() {
                @Override
                public void onSure(String tv) {
                    if (Constans.LANGUAGE_CNA.equals(tv)) {
                        SPUtil.put(SPConfig.LANGUAGE, Locale.SIMPLIFIED_CHINESE.toString());
                        setLocale(Locale.SIMPLIFIED_CHINESE);
                    } else if (Constans.LANGUAGE_TRA.equals(tv)) {
                        SPUtil.put(SPConfig.LANGUAGE, Locale.TRADITIONAL_CHINESE.toString());
                        setLocale(Locale.TRADITIONAL_CHINESE);
                    } else if (Constans.LANGUAGE_EN.equals(tv)) {
                        SPUtil.put(SPConfig.LANGUAGE, Locale.ENGLISH.toString());
                        setLocale(Locale.ENGLISH);
                    }
                    SmartRefreshLayout.setDefaultRefreshHeaderCreator((context, layout) -> new MaterialHeader(context));
                    SmartRefreshLayout.setDefaultRefreshFooterCreator((context, layout) -> new MyClasscsFooter(context));
                    AppManager.getAppManager().finishAllActivity();
                    ARouter.getInstance().build(RConfig.APP_MAIN).navigation();
                }
            });
        });
    }

    public void setLocale(Locale myLocale) {
        Resources res = getResources();
        DisplayMetrics dm = res.getDisplayMetrics();
        Configuration conf = res.getConfiguration();
        conf.locale = myLocale;
        res.updateConfiguration(conf, dm);
        LanguageUtil.saveLanguageSetting(this, myLocale);
    }

    public static void start(Context context) {
        Intent intent = new Intent(context, SettingActivity.class);
        context.startActivity(intent);
    }

    public void checkVersionOK(AppVersionBean versionBean) {
        if (versionBean == null) {
            return;
        }
        String verName = APKVersionCodeUtils.getVerName(this);
        versionBean.setViewStyle(BaseVersion.NOTIFYCATION_STYLE);
        if (APKVersionCodeUtils.compareVersion(versionBean.getVersion(), verName) == 1) {
            android.app.FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction();
            getFragmentManager().beginTransaction();
            DialogFragment appUpDateFragment = (DialogFragment) ARouter.getInstance().build(RConfig.LIB_UPDATE).navigation();
            Bundle bundle = new Bundle();
            bundle.putSerializable(BaseUpdateDialogFragment.INTENT_KEY, versionBean);
            appUpDateFragment.setArguments(bundle);
            appUpDateFragment.show(fragmentTransaction, "update");
        }
    }
}
